package com.adam.jpa.controller;

import com.adam.jpa.domain.User;
import com.adam.jpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@RestController()
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/createUser")
    @ResponseBody
    public User createUser(@RequestParam String name, @RequestParam String email) {
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        userRepository.save(user);
        return user;
    }

    @GetMapping("/findAll")
    @ResponseBody
    public List<User> findAll() {
        Iterable<User> iter = userRepository.findAll();
        List<User> userList = new LinkedList<>();
        iter.forEach(userList::add);
        return userList;
    }

}
